package com.wep.fence.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wep
 * @version 1.0
 * @description:
 * @date 2025/3/19 15:55
 */
@JsonIgnoreProperties(ignoreUnknown = true) //  忽略未知字段，避免异常
@Data
public class RegionGeom {
    private List<RegionFeature> features;
    private String type;
    private String regionCode;
    private String regionName;

    @Data
    public static class RegionFeature {
        private RegionGeometry geometry;
        private String type;
        private RegionProperties properties;
    }

    @Data
    public static class RegionProperties {
        private String name;
        private String area_code;
        private List<Double> center;

        public List<Double> getCenter() {
            if (center == null) {
                center = new ArrayList<>(); // 反序列化后，确保不会是 null
            }
            return center;
        }

        public void setCenter(List<Double> center) {
            this.center = (center == null) ? new ArrayList<>() : center;
        }
    }

    @Data
    public static class RegionGeometry {
        private String type;
        private List<List<List<List<Double>>>> coordinates;

        @Override
        public String toString() {
            try {
                return new ObjectMapper().writeValueAsString(this);
            } catch (JsonProcessingException e) {
                return "{}"; // 解析失败时返回空 JSON
            }
        }
    }
}
